package gao.xiaolei.stream;

import gao.xiaolei.dto.JobkeyDto;
import gao.xiaolei.quertz.QuartzJobService;
import gao.xiaolei.service.TimingService;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@EnableBinding(StreamClient.class)
public class StreamReceiver {

    @Resource(name = "TimingService")
    private TimingService timingService;

    @StreamListener("quertzQueen")
    public void receive(JobkeyDto jobkeyDto) throws SchedulerException {
        System.out.println("执行了");
        System.out.println("接收到消息:".concat(jobkeyDto.toString()));
        if (jobkeyDto.getType() == 1)
            timingService.cancelJobByGroup(jobkeyDto.getGroupId());
        else {
            JobKey jobKey = JobKey.jobKey(jobkeyDto.getKey(), jobkeyDto.getGroupId());
            timingService.cancelJob(jobKey);
        }
    }
}
